package com.ruoyi.system.utils.InterfaceLogUtil;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.system.domain.InterfaceLog;
import com.ruoyi.system.mapper.InterfaceLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description: 异常日志记录工具类
 * @author: zhengxiong.jiang
 * @createDate: 2023/11/2
 * @version: 1.0
 */
@Component
public class InterfaceLogUtil {

    public static final String DIR_MES_MOM = "MES>MOM";
    public static final String DIR_MOM_MES = "MOM>MES";
    public static final String DIR_MOM_WMS = "MOM>WMS";
    public static final String DIR_ERP_MOM = "ERP>MOM";
    public static final String DIR_MOM_ERP = "MOM>ERP";

    public static final String DIR_MOM_SAP = "SAP>MOM";

    public static final String TYPE_RABBITMQ = "rabbitmq";
    public static final String TYPE_HTTP = "http";
    public static final String TYPE_EXCEPTION_WL = "ConnectException: Connection timed out: connect";

    public static final String INTERFACENAME_EXCEPTION_GZXF = "过站信息消费异常";
    public static final String INTERFACENAME_EXCEPTION_GJWLBD = "关键物料绑定消费异常";
    public static final String INTERFACENAME_EXCEPTION_KHTMHC = "客户条码回传失败";
    public static final String INTERFACENAME_EXCEPTION_GJ = "告警信息消费异常";
    public static final String INTERFACENAME_EXCEPTION_BZ = "包装信息消费异常";
    public static final String INTERFACENAME_EXCEPTION_CKSC = "MES华为出库删除异常";


    public static final String INTERFACENAME_CXJL = "产线叫料";
    public static final String INTERFACENAME_FSGDHX = "发送工单信息至MES混线";
    public static final String INTERFACENAME_FSGDZX = "发送工单信息至MES专线";
    public static final String INTERFACENAME_WLTB = "物料主数据同步";
    public static final String INTERFACENAME_WLSCTB = "物料主数据删除同步";
    public static final String INTERFACENAME_GYTB = "ERP工艺数据同步";
    public static final String INTERFACENAME_WLLXTB = "物料主类型同步";
    public static final String INTERFACENAME_WLLXHDTB = "物料主类型回调同步";
    public static final String INTERFACENAME_SAPGDTB = "SAP工单数据同步";
    public static final String INTERFACENAME_JBYC = "解绑异常";

    @Autowired
    public InterfaceLogMapper interfaceLogMapper;


    /**
     * 接口日志记录
     * @param data 请求参数
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param dir 接口请求方向
     * @param type 接口类型
     * @param message 接口返回信息
     * @param interfaceName 接口名称
     */
    public void insertLog(String data,Date startDate,Date endDate,
                          String dir,String type,String message,String interfaceName){
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setInterfaceRequest(data);
        interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog.setStartTime(startDate);
        interfaceLog.setInterfaceDirection(dir);
        interfaceLog.setInterfaceType(type);
        interfaceLog.setEndTime(endDate);
        long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.SECOND);
        interfaceLog.setTimeConsuming(String.valueOf(be));
        interfaceLog.setCreateTime(new Date());
        interfaceLog.setInterfaceResponse(message);
        interfaceLog.setInterfaceName(interfaceName);
        interfaceLogMapper.insert(interfaceLog);
    }



}
